package com.hongyun.tms.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hongyun.tms.common.PageResponseDto;
import com.hongyun.tms.common.annotation.excel.ExcelExport;
import com.hongyun.tms.common.dto.mwork.MWorkItemImportDto;
import com.hongyun.tms.common.dto.mwork.MWorkItemQueryDto;
import com.hongyun.tms.common.dto.mwork.MWorkItemSaveReqDto;
import com.hongyun.tms.common.dto.mwork.MWorkItemTemplateDto;
import com.hongyun.tms.common.utils.MapperingUtils;
import com.hongyun.tms.common.vo.MWorkItemVO;
import com.hongyun.tms.domain.AjaxResult;
import com.hongyun.tms.entity.MWorkItem;
import com.hongyun.tms.entity.PordWork;
import com.hongyun.tms.infra.excel.EasyExcelUtil;
import com.hongyun.tms.infra.excel.listener.WorkItemListener;
import com.hongyun.tms.service.IMWorkItemService;
import com.hongyun.tms.service.IPordWorkService;
import com.hongyun.tms.web.utils.FileUploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 作业项目表 前端控制器
 * </p>
 *
 * @author liPeng
 * @since 2024-06-11
 */
@Api(tags = "作业项目管理")
@RestController
@RequestMapping("/mWorkItem")
public class MWorkItemController {
    @Resource
    private IMWorkItemService workItemService;

    @Resource
    private IPordWorkService workService;
    /**
     * 分页查询
     *
     * @return page
     */
    @GetMapping("list")
    public AjaxResult<PageResponseDto<MWorkItemVO>> list(MWorkItemQueryDto query) {
        PageResponseDto<MWorkItemVO> result = workItemService.pageList(query);
        return AjaxResult.success(result);
    }

    /**
     * 根据ID查询
     *
     * @return MShipVO
     */
    @GetMapping("getById")
    public AjaxResult<MWorkItemVO> getById(@RequestParam("id") Long id) {
        MWorkItem workItem = workItemService.getById(id);
        return AjaxResult.success(MapperingUtils.copyProperties(workItem, MWorkItemVO.class));
    }

    /**
     * 批量删除
     *
     * @return res
     */
    @PostMapping("removeBatch")
    @ApiOperation("批量删除作业项目")
    public AjaxResult removeBatch(@RequestParam(name = "ids") String idList) {
        List<String> ids = Arrays.asList(idList.split(","));
        long count = workService.count(new LambdaQueryWrapper<PordWork>().in(PordWork::getWorkItemId, ids));
        if (count > 0) {
            List<MWorkItem> itemList = workItemService.list(new LambdaQueryWrapper<MWorkItem>().in(MWorkItem::getId, ids));
            return AjaxResult.fail("【" +itemList.get(0).getWorkItem() + "】" + "被作业记录引用，无法删除");
        }
        workItemService.removeByIds(ids);
        return AjaxResult.success();
    }

    /**
     * 新增/修改
     *
     * @return MRateVO
     */
    @PostMapping("save")
    @ApiOperation("新增/修改作业项目")
    public AjaxResult save(@RequestBody @Valid MWorkItemSaveReqDto dto) {
        workItemService.saveOrUpdateWorkItem(dto);
        return AjaxResult.success();
    }

    @ApiOperation("导入作业项")
    @PostMapping("/importWorkItem")
    public AjaxResult<Void> importWorkItem(MultipartFile file) throws IOException {
        AjaxResult<Void> validationResult = FileUploadUtil.validateExcelFile(file);
        // 根据code判断校验是否通过
        if (validationResult.getCode() != HttpStatus.OK.value()) {
            return validationResult;
        }

        // 创建 Excel 数据处理监听器
        WorkItemListener listener = new WorkItemListener(workItemService);

        // 处理 Excel 文件
        try (InputStream inputStream = file.getInputStream()) {
            EasyExcelUtil.syncReadModel(inputStream, listener, MWorkItemImportDto.class, 0, 1);
            return AjaxResult.success();
        }
    }
    @GetMapping("exportTemplate")
    @ExcelExport(value = MWorkItemTemplateDto.class)
    public AjaxResult exportTemplate(HttpServletResponse response) {
        return AjaxResult.success(Collections.emptyList());
    }

    @GetMapping("exportData")
    @ExcelExport(value = MWorkItemVO.class)
    public AjaxResult<List<MWorkItemVO>> exportData(MWorkItemQueryDto query, HttpServletResponse response) {
        List<MWorkItemVO> exportList = workItemService.export(query);
        return AjaxResult.success(exportList);
    }
}
